package com.hisoft.entity;

import org.apache.log4j.Logger;

import com.hisoft.exception.AssertException;
import com.hisoft.type.AssertVerifyType;
import com.hisoft.type.ResultEnum;
import com.hisoft.utility.DataProviderTool;

public class Assert extends ABaseTag {

	Logger logger = Logger.getLogger(Assert.class);
	//期望值
	private String excepted = "";
	//操作
	private ABaseTag verify;
	private AssertVerifyType avt = AssertVerifyType.EQUALS;

	public String getTag() {
		return "Assert";
	}

	public AssertVerifyType getAvt() {
		return avt;
	}

	public void setAvt(AssertVerifyType avt) {
		this.avt = avt;
	}

	public ABaseTag getVerify() {
		return verify;
	}

	public void setVerify(ABaseTag verify) {
		this.verify = verify;
	}

	public String getExcepted() {
		return excepted;
	}

	public void setExcepted(String excepted) {
		this.excepted = excepted;
	}
	
	/**
	 * 数据准备，
	 */
	private void replaceProperty() {
		this.excepted = DataProviderTool.getProperty(excepted);
		if (this.excepted.contains("#$password$#")) {
			this.excepted = this.excepted.replace("#$password$#", DataProviderTool.getProperty("${newPassword}"));
		}
	}
	
	@Override
	public String excute() {
		beforeExcute();
		replaceProperty();
		logger.info(toString());
		String temp = verify.excute();
		String returnStr = Processor(excepted, temp);
		return returnStr;

	}

	/**
	 * 
	 * <b> </b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
	 */
	public String Processor(String excepted, String saw) {
		String info = "";
		switch (avt) {
		case EQUALS:
			if (excepted.equals(saw)) {
				info = "" + ResultEnum.PASS + ": " + saw;
			} else {
				info = "Expected equals'" + excepted + "' but saw '" + saw + "' instead.";
			}
			break;
		case CONTAINS:
			if (saw.contains(excepted)) {
				info = "" + ResultEnum.PASS + ": " + saw;
			} else {
				info = "Expected '" + saw + "' contains '" + excepted + "', but not";
			}
			break;
		case IN:
			if (excepted.contains(saw)) {
				info = "" + ResultEnum.PASS + ": " + saw;
			} else {
				info = "Expected '" + saw + "' in '" + excepted + "', but not";
			}
			break;
		case RESULT:
			if (saw.equals(excepted)) {
				info = "" + ResultEnum.PASS + ": " + saw;
			} else {
				info = "Expected result'" + excepted + "' but saw '" + saw + "' instead.";
			}
			break;
		default:
			break;
		}

		if (info.contains("Expected ")) {
			setResult(ResultEnum.FAIL);
			append(info);
			logger.error(info);
			throw new AssertException(info);
		} else {
			append(info);
			setResult(ResultEnum.PASS);
		}
		return info;
	}

	@Override
	public String toString() {
		return "Verify " + avt.toString().toLowerCase() + " | " + excepted + " | " + verify.toString();
	}
	
	public Assert clone() {
		Assert h = new Assert();
		h.setAvt(this.getAvt());
		h.setExcepted(this.getExcepted());
		h.setLineNumb(this.getLineNumb());
		h.setVerify(this.getVerify());
		h.setInfo(this.getInfo());
		return h;
	}
}
